<?php
/**
 * @author Inhere
 * @version v1.0
 * Use : DI helper
 * Date : 2015-1-10
 * IocHelper.php
 */
namespace ulue\core\di\helpers;

class DiHelper
{
    static public function clearSpace($name)
    {
        return trim( str_replace(' ', '', $name), '. ' );
    }

    // get classname
    static public function getClassname( $class )
    {
        return self::_parseFullClassname( $class, false);
    }

    // get namespace
    static public function getNamespace( $class )
    {
        return self::_parseFullClassname( $class, true);
    }

    /**
     * _parseFullClassname 解析类名
     * @param  mixed $class 类 或 类的实例
     * @param  boolean $type true:namespace false:classname
     * @return string
     */
    static private function _parseFullClassname( $class, $type = true)
    {
        if (is_object($class)) {
            $classname = get_class($class);
        } else if ( !empty($class) && is_string($class) ) {
            $classname = trim($class,'\\ ');
        } else {
            return NULL;
        }

        $classname = trim($classname,'\\ ');

        if ( strpos($classname, '\\')===false ) {
            return $type ? '\\': $classname;
        } else {
            $realClassname = ltrim( strrchr($classname, '\\'), '\\');

            return $type ? substr( $classname, 0, strlen($classname)-strlen($realClassname)-1 ): $realClassname;
        }
    }
}
